package com.me.darkness_is_coming.actors;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;


public class Dragon extends Actor
{
    public enum ColorType {CYAN,GREEN,RED,VIOLET};
    public enum State { UP, DOWN };


    private int wingFrame;
    private int tailFrame;
    private int frame;
    TextureAtlas[] dragonAtlas;
    TextureRegion dragonFrame;

    private Rectangle   colider;

    ColorType color;
    State wingState;
    State tailState;

    private float time;
    private float next_time;



    public Dragon(TextureAtlas[] AtlasMas,ColorType color) {
        dragonFrame = null;

        wingState = State.UP;
        tailState = State.UP;

        wingFrame = 1;
        tailFrame = 3;
        frame = 0;

        time = 0;
        next_time = 10f + (float)Math.random()*10;

        dragonAtlas = AtlasMas;
        this.color = color;

        setSize(Gdx.graphics.getWidth()*0.2f, Gdx.graphics.getHeight()*0.2f*0.397f);
        setPosition(Gdx.graphics.getWidth()*0.1f, Gdx.graphics.getHeight()*0.2f);
        //colider = new Rectangle(getX() + getWidth() * 0.4971f, getY(), getWidth() * (1f-2*0.4971f), getHeight());
        colider = new Rectangle(getX() + getWidth() * 0.4971f, getY(), getWidth() * (1f-2*0.4971f), getHeight());
    }
    private void DefineDragonSprite()
    {
        int wings_frame_number = 15;
        int tail_frame_number = 5;
        String path = "DragonT0";
        if (wingFrame > wings_frame_number)
        {
            wingState = State.DOWN;
            wingFrame--;
        }
        else
        {
            if (wingFrame < 1)
            {
                wingState = State.UP;
                wingFrame++;
            }
        }
        if (frame >  (8 * (float)tail_frame_number / (float)wings_frame_number))
        {
            frame = 0;
            switch (tailFrame)
            {
                case 1:
                    tailState = State.UP;
                    tailFrame = 2;
                    break;
                case 5:
                    tailState = State.DOWN;
                    tailFrame = 4;
                    break;
                default:
                    if (tailState == State.UP) tailFrame++;
                    else tailFrame--;
                    break;
            }
        }
        path += tailFrame + "W";
        if (wingFrame < 10) path += "0";
        path += wingFrame;
        dragonFrame = dragonAtlas[color.ordinal()].findRegion(path);

        if (wingState == State.UP) wingFrame++;
        else wingFrame--;
        frame++;
    }
    public Rectangle getColider(){
        return colider;
    }
    public ColorType getColorType() { return color; }

    @Override
    public void act(float delta){
        time += delta;
        if(time>next_time){
            next_time +=10f + (float)Math.random()*10;
            switch ((int)Math.round(Math.random()*3)){
                case 0:
                    color = ColorType.GREEN;
                    break;
                case 1:
                    color = ColorType.RED;
                    break;
                case 2:
                case 3:
                    color = ColorType.VIOLET;
                    break;
            }
        }


        //float x = Gdx.input.getX();
        float x = getX() - 1.2f*Gdx.graphics.getWidth()*Gdx.input.getAccelerometerX()/4f*delta;
        if(x < 0) x = 0;
        if(x > Gdx.graphics.getWidth()-getWidth())x = Gdx.graphics.getWidth() - getWidth();
        colider.setX(x + getWidth() * 0.4971f);
        setX(x);
    }
    @Override
    public void draw(Batch batch, float parentAlpha)
    {
        DefineDragonSprite();
        batch.draw(dragonFrame, getX(), getY(), getWidth(), getHeight());
    }

    public void dispose()
    {
        for (int i = 0; i < dragonAtlas.length; i++)
            dragonAtlas[i].dispose();
    }
}